package com.atguigu.crowd.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.crowd.entity.po.ProjectPO;
import com.atguigu.crowd.entity.vo.DetailProjectVO;
import com.atguigu.crowd.entity.vo.MemberLauchInfoVO;
import com.atguigu.crowd.entity.vo.PortalProjectVO;
import com.atguigu.crowd.entity.vo.PortalTypeVO;
import com.atguigu.crowd.entity.vo.ProjectVO;
import com.atguigu.crowd.entity.vo.TagVO;
import com.atguigu.crowd.entity.vo.TypeVO;
import com.atguigu.crowd.service.api.ProjectService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;

@RestController
public class ProjectProviderHandler {
	
	@Autowired
	private ProjectService projectService;
	
	// 根据排序条件分页查询项目
	@RequestMapping("/show/projects/order/by/status/remote")
	PageInfo<PortalProjectVO> showProjectOrderByRemote(
			@RequestParam("pageNum") Integer pageNum, 
			@RequestParam("pageSize") Integer pageSize, 
			@RequestParam("keyword") String keyword,
			@RequestParam("factor") String factor){
		
		PageInfo<PortalProjectVO> pageInfo = projectService.getProjectOrderByPageInfo(pageNum, pageSize, keyword,factor);
		return pageInfo;
	}
	
	//根据状态分页查询项目
	@RequestMapping("/show/projects/by/status/remote")
	PageInfo<PortalProjectVO> showProjectByStatusRemote(
			@RequestParam("pageNum") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize,
			@RequestParam("keyword") String keyword,
			@RequestParam("status") Integer status){
		PageInfo<PortalProjectVO> pageInfo = projectService.getProjectByStatusPageInfo(pageNum, pageSize, keyword,status);
		return pageInfo;
	}
	
	//根据分类id分页查询项目
	@RequestMapping("/show/projects/by/typeid/remote")
	PageInfo<PortalProjectVO> showProjectByTypeIdRemote(
			@RequestParam("pageNum") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize,
			@RequestParam("keyword") String keyword,
			@RequestParam("typeId") Integer typeId){
		PageInfo<PortalProjectVO> pageInfo = projectService.getProjectByTypeIdPageInfo(pageNum, pageSize, keyword,typeId);
		return pageInfo;
	}
	
	//分页查询项目
	@RequestMapping("/show/projects/remote")
	PageInfo<PortalProjectVO> showProjectsRemote(
			@RequestParam("pageNum") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize,
			@RequestParam("keyword") String keyword){
		
			PageInfo<PortalProjectVO> pageInfo = projectService.getPageInfo(pageNum, pageSize, keyword);
			return pageInfo;
	}
	
	//更新我发起的项目
	@RequestMapping("/edit/project/detail/remote")
	ResultEntity<String> editProjectDetailRemote(@RequestBody DetailProjectVO detailProjectVO,@RequestParam("projectId") Integer projectId){
		try {
			projectService.updateDetailProject(detailProjectVO,projectId);
			
			return ResultEntity.successWithoutData();
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	//删除我发起的项目
	@RequestMapping("/del/project/detail/remote")
	ResultEntity<String> delProjectDetailRemote(@RequestParam("projectId") Integer projectId){
		try {
			projectService.delDetailProject(projectId);
			
			return ResultEntity.successWithoutData();
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	//查看我发起的项目详情
	@RequestMapping("/show/project/detail/remote")
	ResultEntity<DetailProjectVO> showProjectDetailRemote(@RequestParam("projectId") Integer projectId){
		try {
			DetailProjectVO detailProject  = projectService.getDetailProject(projectId);
			
			return ResultEntity.successWithData(detailProject);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	// 查询项目分类
	@RequestMapping("/get/project/type/vo/remote")
	ResultEntity<List<TypeVO>> getProjectTypeVORemote(){
		try {
			List<TypeVO> projectTypeList  = projectService.getProjectTypeList();
			
			return ResultEntity.successWithData(projectTypeList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	// 查询项目标签
	@RequestMapping("/get/project/tag/vo/remote")
	ResultEntity<List<TagVO>> getProjectTagVORemote(){
		try {
			List<TagVO> projectTagList = projectService.getProjectTagList();
			
			return ResultEntity.successWithData(projectTagList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	//获取我发布的项目
	@RequestMapping("/get/launch/project/remote")
	ResultEntity<List<DetailProjectVO>> getLaunchProjectRemote(@RequestParam("memberId") Integer memberId){
		try {
			List<DetailProjectVO> launchProjectList = projectService.getLaunchProject(memberId);
			
			return ResultEntity.successWithData(launchProjectList);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	//获取发布信息
	@RequestMapping("/get/launch/info/vo/remote/{projectId}")
	ResultEntity<MemberLauchInfoVO> getLaunchInfoVORemote(@PathVariable("projectId") Integer projectId){
		try {
			MemberLauchInfoVO memberLauchInfoVO = projectService.getLaunchInfoVO(projectId);
			
			return ResultEntity.successWithData(memberLauchInfoVO);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	//获取项目详情
	@RequestMapping("/get/project/detail/remote/{projectId}")
	public ResultEntity<DetailProjectVO> getDetailProjectVORemote(@PathVariable("projectId") Integer projectId) {
		
		try {
			DetailProjectVO detailProjectVO = projectService.getDetailProjectVO(projectId);
			
			return ResultEntity.successWithData(detailProjectVO);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
		
	}
	
	
	//保存项目信息到数据库
	@RequestMapping("/save/project/vo/remote")
	ResultEntity<String> saveProjectVORemote(
			@RequestBody ProjectVO projectVO,
			@RequestParam("memberId") Integer memberId){
		try {
			
			projectService.saveProject(projectVO,memberId);
			return ResultEntity.successWithoutData();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultEntity.failed(e.getMessage());
		}
	}
	//查询首页项目，先查询类型，在查询类型下的项目列表
	@RequestMapping("/get/portal/type/project/data/remote")
	ResultEntity<List<PortalTypeVO>> getPortalTypeProjectDataRemote(){
		try {
			
			List<PortalTypeVO> portalTypeVOList = projectService.getPortalTypeVOList();
			return ResultEntity.successWithData(portalTypeVOList);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultEntity.failed(e.getMessage());
		}
	}
	
}
